﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace ReceiveLogDirect
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory()
            {
                HostName = "localhost"
            };
            
            using (var connection = factory.CreateConnection())
            using (var channel=connection.CreateModel())
            {
                channel.ExchangeDeclare(
                    exchange: "direct_logs",
                    type: "direct");
                var queueName = channel.QueueDeclare().QueueName;

                Console.WriteLine("请输入错误类型：error,info,warn.");
                string error = Console.ReadLine();

                channel.QueueBind(
                    queue: queueName, 
                    exchange: "direct_logs", 
                    routingKey: error);
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    var routingKey = ea.RoutingKey;
                    Console.WriteLine(string.Format(" Received {0}:{1}", routingKey, message));
                };
                channel.BasicConsume(
                    queue: queueName, 
                    autoAck: true, 
                    consumer: consumer);

                Console.ReadLine();
            }

        }
    }
}
